/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jigdo.data.template.section;

import com.jcraft.jzlib.*;
import jigdo.JigdoException;
import jigdo.data.template.*;
import java.lang.StringBuilder;

/**
 *
 * @author John R Sohn
 */
public class TemplateDATASection extends TemplateSection {

    public String toString()
    {
        StringBuilder builder = new StringBuilder();
        
        builder.append("Raw Data Section Specifications");
        builder.append("\r\n\r\n");
        
        builder.append("Uncompressed Length: ");
        builder.append(this.UncompressedLength);
        builder.append("\r\n");
        
        builder.append("Uncompressed Data: ");
        builder.append(this.UncompressedData.length);
        builder.append("\r\n");
        
        return builder.toString();
    }
    
    
    public TemplateDATASection(JigdoTemplate parent)
    {
       super(parent);
       this.SectionType = TemplateType.DATA;
    }
    
    /**
     *
     */
    public int UncompressedLength;
    public byte[] UncompressedData;

    @Override
    public void Interpret() throws jigdo.JigdoException {
       
        this.UncompressedLength =
             (int) jigdo.utils.ByteUtils.GetIntFromBytes(DataChunk, 0, 6);
        
        this.UncompressedData = new byte[(int)this.UncompressedLength];

   
        try {
            byte[] b = new byte[this.DataChunk.length-6];
            System.arraycopy(this.DataChunk, 6, b, 0, b.length);
            java.util.zip.Inflater i = new java.util.zip.Inflater(false);
            i.setInput(b);
            // i.setInput(this.DataChunk,6,this.DataChunk.length-6);
            i.inflate(this.UncompressedData);
         //   System.out.println("Decompressed Data Stream");
        }
        catch (java.util.zip.DataFormatException e) {
            throw new JigdoException("Error encountered when decompressing DATA section", e);
        }
        
        if (!TemplateSection.RetainDataChunk)
        {
            this.DataChunk = null;
        }

    }
}
